package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.*;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.user.UserBuyService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/ajax/course")
public class AjaxCoursePayController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AjaxCoursePayController.class);
	// 课程播放

	@Autowired
	private UserBuyService userBuyService;
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private WebsiteProfileService websiteProfileService;

	/**
	 * 课程章节 检查 播放
	 */
	@RequestMapping("/play/address.json")
	@ResponseBody
	public AjaxResult nodePay(HttpServletRequest request) {
		int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
		User user = LoginUser.getUser(request);
		if (user == null) {
			return AjaxResult.error("您还未登录，请登录后再观看！");
		}
		CourseNode courseNode = courseNodeService.get(nodeId);
		if (courseNode == null) {
			return AjaxResult.error("参数错误！");
		}
		Course course = courseService.get(courseNode.getCourseId());
		if (course == null) {
			return AjaxResult.error("参数错误！");
		}
		int courseId = course.getCourseId();
		try {
			// 判断节点不可以试听
			if (course.getPrice() > 0 && courseNode.getFree() != 1) {
				boolean isBuy = userBuyService.checkIsBuy(user.getUserId(), ModuleType.COURSE.ordinal(),courseId);
				if (!isBuy) {// 判断该课程不可以观看
					return AjaxResult.error("您还未报名此课程，请报名报观看。");
				}
			}
			//视频
			if ("VIDEO".equals(courseNode.getFileType())) {
				// 视频url
				String videoUrl = null;
				// 播放类型
				String videoType = courseNode.getVideoType();
				//如果是阿里云云点播
				if (videoType.equalsIgnoreCase("ALIYUNVIDEO")) {
					Map<String, Object> map = websiteProfileService.getMap(WebsiteProfileType.aliyun);
					videoUrl = AliyunUtils.getPlayURL(map.get("accessKeyId").toString(), map.get("accessKeySecret").toString(), TKString.trim(courseNode.getNodeKey()));
				}
				/*如果是百家云点播*/
				if (videoType.equalsIgnoreCase(WebsiteProfileType.baiJiaYun.toString())) {
					Map<String, Object> baiJiaYun = websiteProfileService.getMap(WebsiteProfileType.baiJiaYun);
					String videoMsg = BaJiaYunUtils.getVideoUrl(TKString.trim(courseNode.getNodeKey()), baiJiaYun);
					Map<String, Object> videoMap = FastJsonUtils.json2Map(videoMsg);
					int code = TKConvert.toInteger(videoMap.get("code").toString());
					/*如果code是0说明成功*/
					if (code == 0) {
						Map<String, Object> dataMmap = FastJsonUtils.json2Map(videoMap.get("data").toString());
						//System.out.println("dataMmap=" + dataMmap);
						Map<String, Object> playMmap = FastJsonUtils.json2Map(dataMmap.get("play_info").toString());
						//System.out.println("playMmap=" + playMmap);
						Map<String, Object> urlMap = FastJsonUtils.json2Map(playMmap.get("superHD").toString());
						videoUrl = urlMap.get("url") + "";
					} else {
						return AjaxResult.error("视频地址错误，请检查修改后重试!");
					}

				}
				return AjaxResult.success(videoUrl);
			} else if (courseNode.getFileType().equals("AUDIO")) {
				// 音频url
				String videoUrl = null;
				// 播放类型
				String audioType = courseNode.getVideoType();
				//如果是阿里云云点播
				if (audioType.equalsIgnoreCase("ALIYUNVIDEO")) {
					Map<String, Object> map = websiteProfileService.getMap(WebsiteProfileType.aliyun);
					videoUrl = AliyunUtils.getPlayURL(map, courseNode.getNodeKey());
				}
				/*如果是百家云点播*/
				if (audioType.equalsIgnoreCase(WebsiteProfileType.baiJiaYun.toString())) {
					Map<String, Object> baiJiaYun = websiteProfileService.getMap(WebsiteProfileType.baiJiaYun);
					String videoMsg = BaJiaYunUtils.getVideoUrl(courseNode.getNodeKey(), baiJiaYun);
					Map<String, Object> videoMap = FastJsonUtils.json2Map(videoMsg);
					int code = TKConvert.toInteger(videoMap.get("code").toString());
					/*如果code是0说明成功*/
					if (code == 0) {
						Map<String, Object> dataMmap = FastJsonUtils.json2Map(videoMap.get("data").toString());
						Map<String, Object> playMmap = FastJsonUtils.json2Map(dataMmap.get("play_info").toString());
						Map<String, Object> urlMap = FastJsonUtils.json2Map(playMmap.get("low").toString());
						videoUrl = urlMap.get("url").toString();
					} else {
						return AjaxResult.error("音频地址错误，请检查修改后重试!");
					}
				}
				return AjaxResult.success(videoUrl);
			} else {
				return AjaxResult.error("播放类型异常！");
			}
		} catch (Exception e) {
			logger.error("CourseNodeController.getNodeLink", e);
			return AjaxResult.error("系统繁忙,请稍后再试！");
		}
	}
}